package com.whimscrew.zuul.filters;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//@Component
public class TrackingFilter extends ZuulFilter {
    private static final int      FILTER_ORDER =  1;
    private static final boolean  SHOULD_FILTER=true;
    private static final Logger logger = LoggerFactory.getLogger(TrackingFilter.class);

    @Autowired
    FilterUtils filterUtils;

    /**
     * 过滤器类型：前置过滤器
     */
    @Override
    public String filterType() {
        return FilterUtils.PRE_FILTER_TYPE;
    }

    @Override
    public int filterOrder() {
        return FILTER_ORDER;
    }

    public boolean shouldFilter() {
        return SHOULD_FILTER;
    }

    private boolean isAuth(){
      if (filterUtils.getAuthToken() !=null){
          return true;
      }

      return false;
    }

    public Object run() {
        if (!isAuth()) {
            RequestContext requestContext = RequestContext.getCurrentContext();
            requestContext.setResponseStatusCode(500);
            requestContext.setResponseBody("不合法的token");
           return null;
        }
        logger.debug(" =================Token found in tracking filter: {}. ", filterUtils.getAuthToken());
        return null;
    }
}